package com.lch.thread.juc;

import java.util.concurrent.Semaphore;

/**
 * @author: liuchenhui
 * @create: 2019-05-20 22:08
 **/
public class SemaphoreDemo extends Thread {

    private int num;
    private Semaphore semaphore;

    public SemaphoreDemo(int num, Semaphore semaphore) {
        this.num = num;
        this.semaphore = semaphore;
    }

    @Override
    public void run() {
        try {
            semaphore.acquire();
            System.out.println("第" + num + "车开进去了");
            Thread.sleep(1000L);
            System.out.println("第" + num + "车开走了");
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {

        }
    }

    public static void main(String[] args) {
        final Semaphore semaphore = new Semaphore(5);
        for (int i = 0; i < 10; i++) {
            new SemaphoreDemo(i, semaphore).start();
        }
    }
}
